using System.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace <Application>.<Presentation>
{
    public partial class frm<OwnerU>_<TableU> : UserControl
    {
        public frm<OwnerU>_<TableU>()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(frm<OwnerU>_<TableU>_Loaded);
        }

        private bool AddMode = false;
        private bool EditMode = false;
        private bool DeleteMode = false;
        private int Row = 0;

        <Database>DataClass cls<Database>Data = new <Database>DataClass();
        public <DRType> <DRField>;
        
        private void frm<OwnerU>_<TableU>_Loaded(object sender, RoutedEventArgs e)
        {
            cmbFields.Items.Clear();
    	<If Loop>
   		<IsNotText>
   		<And>
   		<IsNotNText>
            cmbFields.Items.Add("<FieldC>");
    	</If Loop>

            cmbCondition.Items.Clear();
            cmbCondition.Items.Add("Contains");
            cmbCondition.Items.Add("Equals");
            cmbCondition.Items.Add("Starts with...");
            cmbCondition.Items.Add("More than...");
            cmbCondition.Items.Add("Less than...");
            cmbCondition.Items.Add("Equal or more than...");
            cmbCondition.Items.Add("Equal or less than...");

            InitToolBarItems();
            tiGrid.Height = 0;
            tiDetail.Height = 0;
        <If Loop>
          <IsFKey>
          <And>
          <IsComboBox (Db)>
            Load<OwnerU>_<TableU>_<ROwnerU>_<RTableU>ComboBox<MiscTag>();
        </If Loop>
            LoadGrid(true);
            tiGrid.IsSelected = true;
        }

        <If Loop>
        <IsFKey>
        <And>
        <IsComboBox (Db)>
        private void Load<OwnerU>_<TableU>_<ROwnerU>_<RTableU>ComboBox<MiscTag>()
        {
            using (new WaitCursor())
            {
                List<<OwnerU>_<TableU>_<ROwnerU>_<RTableU>Class<MiscTag>> <OwnerU>_<TableU>_<ROwnerU>_<RTableU>List = new  List<<OwnerU>_<TableU>_<ROwnerU>_<RTableU>Class<MiscTag>>();
                try
                {
                    <OwnerU>_<TableU>_<ROwnerU>_<RTableU>List = <OwnerU>_<TableU>_<ROwnerU>_<RTableU>DataClass<MiscTag>.List();
                    <IsFKey>
                    <And>
                    <IsComboBox (Db)>
                    cb<FieldU>.ItemsSource = <OwnerU>_<TableU>_<ROwnerU>_<RTableU>List;
                    cb<FieldU>.DisplayMemberPath = "<RDisplayFieldU>";
                    cb<FieldU>.SelectedValuePath = "<RFieldU>";
                }
                catch (System.Exception ex)
                {
                     MessageBox.Show(ex.Message, ex.GetType().ToString());
                }
            }
        }

        </If Loop>
        public void LoadGrid(bool SelectRow)
        {
            using (new WaitCursor())
            {
                try
                {
                    DataTable dt = <OwnerU>_<TableU>DataClass.Select_Records(System.Convert.To<DRType>(<DRField>));
                    Grid.ItemsSource = dt.DefaultView;
                    Grid.CanUserAddRows = false;
                    Grid.CanUserDeleteRows = false;
                    Grid.IsReadOnly = true;
                    if (Grid.Items.Count > 0 & SelectRow == true)
                    {
                        Grid.SelectedItem = Grid.Items[0];
                        Grid.ScrollIntoView(Grid.SelectedItem);
                    }
                }
                catch (System.Exception ex)
                {
                     MessageBox.Show(ex.Message, ex.GetType().ToString());
                }
                finally
                {
                }
            }
        }

        private void Grid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            string headername = e.Column.Header.ToString();
            DataGridTextColumn txtCol = e.Column as DataGridTextColumn;
    <If Loop>
        <IsDateTimePicker>
            if (headername == "<FieldC>")
            {
                txtCol.Binding.StringFormat = "{0:d}";
            }
        <IsMoney>
            if (headername == "<FieldC>")
            {
                txtCol.Binding.StringFormat = "{0:C2}";
                txtCol.ElementStyle = (System.Windows.Style)Grid.FindResource("rightAlignment");
            }
        <IsSmallMoney>
            if (headername == "<FieldC>")
            {
                txtCol.Binding.StringFormat = "{0:C2}";
                txtCol.ElementStyle = (System.Windows.Style)Grid.FindResource("rightAlignment");
            }
        <IsNumber>
        <And>
        <IsNotFKey>
        <And>
        <IsNotMoney>
        <And>
        <IsNotSmallMoney>
        <And>
        <IsNotPKey>
        <And>
        <IsNotIdentity>
            if (headername == "<FieldC>")
            {
                txtCol.Binding.StringFormat = "{0:N<DecimalPlaces>}";
                txtCol.ElementStyle = (System.Windows.Style)Grid.FindResource("rightAlignment");
            }
    </If Loop>
        }

        private void tbiAdd_Click(object sender, RoutedEventArgs e)
        {
            Add();
            tiDetail.IsSelected = true;

            InitToolBarItems();
            tbiAdd.IsEnabled = false;
            tbiEdit.IsEnabled = false;
            tbiDelete.IsEnabled = false;
            tbiApply.IsEnabled = true;
            tbiCancel.IsEnabled = true;
            tbiRefresh.IsEnabled = false;
        }

        private void tbiEdit_Click(object sender, RoutedEventArgs e)
        {
            Edit();
            tiDetail.IsSelected = true;

            InitToolBarItems();
            tbiAdd.IsEnabled = false;
            tbiEdit.IsEnabled = false;
            tbiDelete.IsEnabled = false;
            tbiApply.IsEnabled = true;
            tbiCancel.IsEnabled = true;
            tbiRefresh.IsEnabled = false;
        }

        private void tbiDelete_Click(object sender, RoutedEventArgs e)
        {
            Delete();
            tiDetail.IsSelected = true;

            InitToolBarItems();
            tbiAdd.IsEnabled = false;
            tbiEdit.IsEnabled = false;
            tbiDelete.IsEnabled = false;
            tbiApply.IsEnabled = true;
            tbiCancel.IsEnabled = true;
            tbiRefresh.IsEnabled = false;
        }

        private void tbiApply_Click(object sender, RoutedEventArgs e)
        {
            if (this.AddMode == true)
            {
                this.InsertRecord();
            }
            else if (this.EditMode == true)
            {
                this.UpdateRecord();
            }
            else if (this.DeleteMode == true)
            {
                this.DeleteRecord();
            }
        }

        private void tbiCancel_Click(object sender, RoutedEventArgs e)
        {
            tiGrid.IsSelected = true;
            InitToolBarItems();
        }

        private void tbiRefresh_Click(object sender, RoutedEventArgs e)
        {
            LoadGrid(true);
        }

        private void Grid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Row = Grid.SelectedIndex;
        }
        
        private void InitToolBarItems()
        {
            tbiAdd.IsEnabled = true;
            tbiEdit.IsEnabled = true;
            tbiDelete.IsEnabled = true;
            tbiApply.IsEnabled = false;
            tbiCancel.IsEnabled = false;
            tbiRefresh.IsEnabled = true;
        }

        private void GetData()
        {
            using (new WaitCursor())
            {
                ClearRecord();

                <OwnerU>_<TableU>Class cls<OwnerU>_<TableU> = new <OwnerU>_<TableU>Class();
        <If Loop>
           <IsPKey>
                cls<OwnerU>_<TableU>.<FieldU> = <Convert>((Grid.SelectedCells[<ColNo>].Column.GetCellContent(Grid.SelectedItem) as TextBlock).Text);
        </If Loop>
        <If Loop>
           <IsNotPKey>
           <And>
           <IsIdentity>
                cls<OwnerU>_<TableU>.<FieldU> = <Convert>((Grid.SelectedCells[<ColNo>].Column.GetCellContent(Grid.SelectedItem) as TextBlock).Text);
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
                cls<OwnerU>_<TableU>.<FieldU> = <Convert>((Grid.SelectedCells[<ColNo>].Column.GetCellContent(Grid.SelectedItem) as TextBlock).Text);
           <ExitLoop>
        </If Loop>
                cls<OwnerU>_<TableU> = <OwnerU>_<TableU>DataClass.Select_Record(cls<OwnerU>_<TableU>);

                if ((cls<OwnerU>_<TableU> != null))
                {
                    try
                    {
                <If Loop>
                       <IsCheckBox>
                       <And>
                       <IsNull>
                        if (cls<OwnerU>_<TableU>.<Field> is null) { cb<FieldU>.IsChecked = default(<CSType>); } else { cb<FieldU>.IsChecked = <Convert>(cls<OwnerU>_<TableU>.<Field>); }
                       <IsNumericUpDown>
                       <And>
                       <IsNull>
                        if (cls<OwnerU>_<TableU>.<Field> is null) { nud<FieldU>.Text = default(string); } else { nud<FieldU>.Text = <Convert>(cls<OwnerU>_<TableU>.<Field>).ToString(); }
                       <IsDateTimePicker>
 	               <And>
                       <IsNotDateTimeOffset>
 	               <And>
                       <IsNotTime>
                       <And>
                       <IsNull>
                        if (cls<OwnerU>_<TableU>.<Field> is null) { dtp<FieldU>.SelectedDate = DateTime.Now; } else { dtp<FieldU>.SelectedDate = System.Convert.ToDateTime(cls<OwnerU>_<TableU>.<FieldU>); }
                       <IsDateTimePicker>
 	               <And>
                       <IsDateTimeOffset>
 	               <And>
                       <IsNotTime>
                       <And>
                       <IsNull>
                        if (cls<OwnerU>_<TableU>.<Field> is null) { dtp<FieldU>.SelectedDate = DateTime.Now; } else { dtp<FieldU>.SelectedDate = <Convert>(cls<OwnerU>_<TableU>.<FieldU>); }
                       <IsDateTimePicker>
 	               <And>
                       <IsNotDateTimeOffset>
 	               <And>
                       <IsTime>
                       <And>
                       <IsNull>
                        if (cls<OwnerU>_<TableU>.<Field> is null) { dtp<FieldU>.SelectedDate = DateTime.Now; } else { dtp<FieldU>.SelectedDate = System.Convert.ToDateTime(cls<OwnerU>_<TableU>.<FieldU>); }
                       <IsTextBox>
                       <And>
                       <IsNull>
                        if (cls<OwnerU>_<TableU>.<Field> is null) { tb<FieldU>.Text = default(string); } else { tb<FieldU>.Text = Convert.ToString(cls<OwnerU>_<TableU>.<FieldU>); }
                       <IsMaskedTextBox>
                       <And>
                       <IsNull>
                        if (cls<OwnerU>_<TableU>.<Field> is null) { mtb<FieldU>.Text = default(string); } else { mtb<FieldU>.Text = Convert.ToString(cls<OwnerU>_<TableU>.<FieldU>); }
                       <IsMultilineTextBox>
                       <And>
                       <IsNull>
                        if (cls<OwnerU>_<TableU>.<Field> is null) { mtb<FieldU>.Text = default(string); } else { mtb<FieldU>.Text = Convert.ToString(cls<OwnerU>_<TableU>.<FieldU>); }
                       <IsRichTextBox>
                       <And>
                       <IsNull>
                        if (cls<OwnerU>_<TableU>.<Field> is null) { rtb<FieldU>.Text = default(string); } else { rtb<FieldU>.Text = Convert.ToString(cls<OwnerU>_<TableU>.<FieldU>); }
                       <IsPictureBox>
                       <And>
                       <IsNull>
                        if (cls<OwnerU>_<TableU>.<Field> is null) { pb<FieldU>.Value = default(<CSType>); } else { pb<FieldU>.Value = <Convert>(cls<OwnerU>_<TableU>.<Field>); }
                       <IsComboBox>
                       <And>
                       <IsNull>
                        if (cls<OwnerU>_<TableU>.<Field> is null) { cb<FieldU>.Text = default(string); } else { cb<FieldU>.Text = Convert.ToString(cls<OwnerU>_<TableU>.<FieldU>); }
                       <IsComboBox (Db)>
                       <And>
                       <IsNull>
                        if (cls<OwnerU>_<TableU>.<Field> is null) { cb<FieldU>.SelectedValue = default(<CSType>); } else { cb<FieldU>.SelectedValue = <Convert>(cls<OwnerU>_<TableU>.<FieldU>); }
--------------------------------------------------------------------------------------------------------<Comment>
                       <IsCheckBox>
                       <And>
                       <IsNotNull>
                        cb<FieldU>.IsChecked = <Convert>(cls<OwnerU>_<TableU>.<Field>);
                       <IsNumericUpDown>
                       <And>
                       <IsNotNull>
                        nud<FieldU>.Text = <Convert>(cls<OwnerU>_<TableU>.<Field>).ToString();
                       <IsDateTimePicker>
 	               <And>
                       <IsNotDateTimeOffset>
 	               <And>
                       <IsNotTime>
                       <And>
                       <IsNotNull>
                        dtp<FieldU>.SelectedDate = System.Convert.ToDateTime(cls<OwnerU>_<TableU>.<FieldU>);
                       <IsDateTimePicker>
 	               <And>
                       <IsDateTimeOffset>
 	               <And>
                       <IsNotTime>
                       <And>
                       <IsNotNull>
                        dtp<FieldU>.SelectedDate = <Convert>(cls<OwnerU>_<TableU>.<FieldU>);
                       <IsDateTimePicker>
 	               <And>
                       <IsNotDateTimeOffset>
 	               <And>
                       <IsTime>
                       <And>
                       <IsNotNull>
                        dtp<FieldU>.SelectedDate = System.Convert.ToDateTime(cls<OwnerU>_<TableU>.<FieldU>);
                       <IsTextBox>
                       <And>
                       <IsNotNull>
                        tb<FieldU>.Text = Convert.ToString(cls<OwnerU>_<TableU>.<FieldU>);
                       <IsMaskedTextBox>
                       <And>
                       <IsNotNull>
                        mtb<FieldU>.Text = Convert.ToString(cls<OwnerU>_<TableU>.<FieldU>);
                       <IsMultilineTextBox>
                       <And>
                       <IsNotNull>
                        mtb<FieldU>.Text = Convert.ToString(cls<OwnerU>_<TableU>.<FieldU>);
                       <IsRichTextBox>
                       <And>
                       <IsNotNull>
                        rtb<FieldU>.Text = Convert.ToString(cls<OwnerU>_<TableU>.<FieldU>);
                       <IsPictureBox>
                       <And>
                       <IsNotNull>
                        pb<FieldU>.Value = <Convert>(cls<OwnerU>_<TableU>.<Field>);
                       <IsComboBox>
                       <And>
                       <IsNotNull>
                        cb<FieldU>.Text = Convert.ToString(cls<OwnerU>_<TableU>.<FieldU>);
                       <IsComboBox (Db)>
                       <And>
                       <IsNotNull>
                        cb<FieldU>.SelectedValue = <Convert>(cls<OwnerU>_<TableU>.<FieldU>);
                </If Loop>
                    }
                    catch
                    {
                    }
                }
            }
        }

        private void SetData(<OwnerU>_<TableU>Class cls<OwnerU>_<TableU>)
        {
            using (new WaitCursor())
            {
                <If Loop>
                    <IsNotIdentity>
                    <And>
                    <IsCheckBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
                cls<OwnerU>_<TableU>.<FieldU> = <Convert>(cb<FieldU>.IsChecked) ? true : false;                        
                    <IsNotIdentity>
                    <And>
                    <IsNumericUpDown>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
                if (string.IsNullOrEmpty(nud<FieldU>.Text)) {
                    cls<OwnerU>_<TableU>.<FieldU> = null;
                } else {
                    cls<OwnerU>_<TableU>.<FieldU> = <Convert>(nud<FieldU>.Text); }
                    <IsNotIdentity>
                    <And>
                    <IsDateTimePicker>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
                if (string.IsNullOrEmpty(dtp<FieldU>.Text)) {
                    cls<OwnerU>_<TableU>.<FieldU> = null;
                } else {
                    cls<OwnerU>_<TableU>.<FieldU> = <Convert>(dtp<FieldU>.Text); }
                    <IsNotIdentity>
                    <And>
                    <IsTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
                if (string.IsNullOrEmpty(tb<FieldU>.Text)) {
                    cls<OwnerU>_<TableU>.<FieldU> = null;
                } else {
                    cls<OwnerU>_<TableU>.<FieldU> = <Convert>(tb<FieldU>.Text); }
                    <IsNotIdentity>
                    <And>
                    <IsMaskedTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
                if (string.IsNullOrEmpty(mtb<FieldU>.Text)) {
                    cls<OwnerU>_<TableU>.<FieldU> = null;
                } else {
                    cls<OwnerU>_<TableU>.<FieldU> = <Convert>(mtb<FieldU>.Text); }
                    <IsNotIdentity>
                    <And>
                    <IsMultilineTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
                if (string.IsNullOrEmpty(mtb<FieldU>.Text)) {
                    cls<OwnerU>_<TableU>.<FieldU> = null;
                } else {
                    cls<OwnerU>_<TableU>.<FieldU> = <Convert>(mtb<FieldU>.Text); }
                    <IsNotIdentity>
                    <And>
                    <IsRichTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
                if (string.IsNullOrEmpty(rtb<FieldU>.Text)) {
                    cls<OwnerU>_<TableU>.<FieldU> = null;
                } else {
                    cls<OwnerU>_<TableU>.<FieldU> = <Convert>(rtb<FieldU>.Text); }
                    <IsNotIdentity>
                    <And>
                    <IsPictureBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
                        
                    <IsNotIdentity>
                    <And>
                    <IsComboBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
                cls<OwnerU>_<TableU>.<FieldU> = <Convert>(cb<FieldU>.Text);
                    <IsNotIdentity>
                    <And>
                    <IsComboBox (Db)>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
                if (string.IsNullOrEmpty(cb<FieldU>.SelectedValue.ToString())) {
                    cls<OwnerU>_<TableU>.<FieldU> = null;
                } else {
                    cls<OwnerU>_<TableU>.<FieldU> = <Convert>(cb<FieldU>.SelectedValue); }
--------------------------------------------------------------------------------------------------------<Comment>
                    <IsNotIdentity>
                    <And>
                    <IsCheckBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
                cls<OwnerU>_<TableU>.<FieldU> = <Convert>(cb<FieldU>.IsChecked) ? true : false;  
                    <IsNotIdentity>
                    <And>
                    <IsNumericUpDown>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
                if (string.IsNullOrEmpty(nud<FieldU>.Text)) {
                    cls<OwnerU>_<TableU>.<FieldU> = null;
                } else {
                    cls<OwnerU>_<TableU>.<FieldU> = <Convert>(nud<FieldU>.Text); }
                    <IsNotIdentity>
                    <And>
                    <IsDateTimePicker>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
                if (string.IsNullOrEmpty(dtp<FieldU>.Text)) {
                    cls<OwnerU>_<TableU>.<FieldU> = null;
                } else {
                    cls<OwnerU>_<TableU>.<FieldU> = <Convert>(dtp<FieldU>.Text); }
                    <IsNotIdentity>
                    <And>
                    <IsTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
                if (string.IsNullOrEmpty(tb<FieldU>.Text)) {
                    cls<OwnerU>_<TableU>.<FieldU> = null;
                } else {
                    cls<OwnerU>_<TableU>.<FieldU> = tb<FieldU>.Text; }
                    <IsNotIdentity>
                    <And>
                    <IsMaskedTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
                if (string.IsNullOrEmpty(mtb<FieldU>.Text)) {
                    cls<OwnerU>_<TableU>.<FieldU> = null;
                } else {
                    cls<OwnerU>_<TableU>.<FieldU> = mtb<FieldU>.Text; }
                    <IsNotIdentity>
                    <And>
                    <IsMultilineTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
                if (string.IsNullOrEmpty(mtb<FieldU>.Text)) {
                    cls<OwnerU>_<TableU>.<FieldU> = null;
                } else {
                    cls<OwnerU>_<TableU>.<FieldU> = mtb<FieldU>.Text; }
                    <IsNotIdentity>
                    <And>
                    <IsRichTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
                if (string.IsNullOrEmpty(rtb<FieldU>.Text)) {
                    cls<OwnerU>_<TableU>.<FieldU> = null;
                } else {
                    cls<OwnerU>_<TableU>.<FieldU> = rtb<FieldU>.Text; }
                    <IsNotIdentity>
                    <And>
                    <IsPictureBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
                        
                    <IsNotIdentity>
                    <And>
                    <IsComboBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
                cls<OwnerU>_<TableU>.<FieldU> = <Convert>(cb<FieldU>.Text);
                    <IsNotIdentity>
                    <And>
                    <IsComboBox (Db)>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
                if (string.IsNullOrEmpty(cb<FieldU>.SelectedValue.ToString())) {
                    cls<OwnerU>_<TableU>.<FieldU> = null;
                } else {
                    cls<OwnerU>_<TableU>.<FieldU> = <Convert>(cb<FieldU>.SelectedValue); }
--------------------------------------------------------------------------------------------------------<Comment>
                    <IsNotIdentity>
                    <And>
                    <IsCheckBox>
                    <And>
                    <IsNotNull>
                cls<OwnerU>_<TableU>.<FieldU> = <Convert>(<Convert>(cb<FieldU>.IsChecked) ? true : false);
                    <IsNotIdentity>
                    <And>
                    <IsNumericUpDown>
                    <And>
                    <IsNotNull>
                cls<OwnerU>_<TableU>.<FieldU> = <Convert>(nud<FieldU>.Text);
                    <IsNotIdentity>
                    <And>
                    <IsDateTimePicker>
                    <And>
                    <IsNotNull>
                cls<OwnerU>_<TableU>.<FieldU> = <Convert>(dtp<FieldU>.Text);
                    <IsNotIdentity>
                    <And>
                    <IsTextBox>
                    <And>
                    <IsNotNull>
                cls<OwnerU>_<TableU>.<FieldU> = <Convert>(tb<FieldU>.Text);
                    <IsNotIdentity>
                    <And>
                    <IsMaskedTextBox>
                    <And>
                    <IsNotNull>
                cls<OwnerU>_<TableU>.<FieldU> = <Convert>(mtb<FieldU>.Text);
                    <IsNotIdentity>
                    <And>
                    <IsMultilineTextBox>
                    <And>
                    <IsNotNull>
                cls<OwnerU>_<TableU>.<FieldU> = <Convert>(mtb<FieldU>.Text);
                    <IsNotIdentity>
                    <And>
                    <IsRichTextBox>
                    <And>
                    <IsNotNull>
                cls<OwnerU>_<TableU>.<FieldU> = <Convert>(rtb<FieldU>.Text);
                    <IsNotIdentity>
                    <And>
                    <IsPictureBox>
                    <And>
                    <IsNotNull>
                        
                    <IsNotIdentity>
                    <And>
                    <IsComboBox>
                    <And>
                    <IsNotNull>
                cls<OwnerU>_<TableU>.<FieldU> = <Convert>(cb<FieldU>.Text);
                    <IsNotIdentity>
                    <And>
                    <IsComboBox (Db)>
                    <And>
                    <IsNotNull>
                cls<OwnerU>_<TableU>.<FieldU> = <Convert>(cb<FieldU>.SelectedValue);
--------------------------------------------------------------------------------------------------------<Comment>
                </If Loop>
            }
        }

        private void Add()
        {
            this.AddMode = true;
            this.EditMode = false;
            this.DeleteMode = false;

            ClearRecord();
            IsReadOnlyRecord(true);
 -----------------------------------------------------<Comment>
            <If Loop>
            <IsUniqueIdentifier>
            <And>
            <IsTextBox>
            this.tb<FieldU>.Text = Guid.NewGuid().ToString();
            <IsUniqueIdentifier>
            <And>
            <IsMaskedTextBox>
            this.tb<FieldU>.Text = Guid.NewGuid().ToString();
            </If Loop>
 -----------------------------------------------------<Comment>
            <If Loop>
           <IsNotIdentity>
            <And>
            <IsCheckBox>
            cb<FieldU>.Focus();
            <ExitLoop>
           <IsNotIdentity>
            <And>
            <IsNumericUpDown>
            nud<FieldU>.Focus();
            <ExitLoop>
           <IsNotIdentity>
            <And>
            <IsDateTimePicker>
            dtp<FieldU>.Focus();
            <ExitLoop>
           <IsNotIdentity>
            <And>
            <IsTextBox>
            tb<FieldU>.Focus();
            <ExitLoop>
           <IsNotIdentity>
            <And>
            <IsMaskedTextBox>
            mtb<FieldU>.Focus();
            <ExitLoop>
           <IsNotIdentity>
            <And>
            <IsMultilineTextBox>
            mtb<FieldU>.Focus();
            <ExitLoop>
           <IsNotIdentity>
            <And>
            <IsRichTextBox>
            rtb<FieldU>.Focus();
            <ExitLoop>
           <IsNotIdentity>
            <And>
            <IsPictureBox>
            pb<FieldU>.Focus();
            <ExitLoop>
           <IsNotIdentity>
            <And>
            <IsComboBox>
            cb<FieldU>.Focus();
            <ExitLoop>
           <IsNotIdentity>
            <And>
            <IsComboBox (Db)>
            cb<FieldU>.Focus();
            <ExitLoop>
            </If Loop>
-----------------------------------------------------<Comment>
            <If Loop>
               <IsIdentity>
               <And>
               <IsNumericUpDown>
            nud<FieldU>.Text = System.Convert.ToString(cls<Database>Data.getAutoID("New", "<Table>"));
            </If Loop>
        <If Loop>
           <IsFKey>
           <And>
           <IsNotComboBox (Db)>
           <And>
           <IsNumber>
            nud<FieldU>.IsEnabled = false;
            nud<FieldU>.Text = System.Convert.ToString(<DRField>);
           <IsFKey>
           <And>
           <IsNotComboBox (Db)>
           <And>
           <IsNotNumber>
            tb<FieldU>.IsEnabled = false;
            tb<FieldU>.Text = System.Convert.ToString(<DRField>);
        </If Loop>
        }

        private void Edit()
        {
            AddMode = false;
            EditMode = true;
            DeleteMode = false;

            GetData();

            IsReadOnlyRecord(true);
            <If Loop>
            <IsNotIdentity>
            <And>
            <IsPKey>
            <And>
            <IsNumericUpDown>
            nud<FieldU>.IsEnabled = false;
            <IsNotIdentity>
            <And>
            <IsPKey>
            <And>
            <IsTextBox>
            tb<FieldU>.IsEnabled = false;
            <IsNotIdentity>
            <And>
            <IsPKey>
            <And>
            <IsMaskedTextBox>
            mtb<FieldU>.IsEnabled = false;
            <IsNotIdentity>
            <And>
            <IsPKey>
            <And>
            <IsMultilineTextBox>
            mtb<FieldU>.IsEnabled = false;
            <IsNotIdentity>
            <And>
            <IsPKey>
            <And>
            <IsRichTextBox>
            rtb<FieldU>.IsEnabled = false;
            <IsNotIdentity>
            <And>
            <IsPKey>
            <And>
            <IsComboBox (Db)>
            cb<FieldU>.IsEnabled = false;
            <IsNotIdentity>
            <And>
            </If Loop>
            <If Loop>
            <IsNotIdentityCount>
            <And>
            <IsNotPKeyCount>
            <And>
            <IsNumericUpDown>
            nud<FieldU>.IsEnabled = false;
            <ExitLoop>
            <IsNotIdentityCount>
            <And>
            <IsNotPKeyCount>
            <And>
            <IsTextBox>
            tb<FieldU>.IsEnabled = false;
            <ExitLoop>
            <IsNotIdentityCount>
            <And>
            <IsNotPKeyCount>
            <And>
            <IsMaskedTextBox>
            mtb<FieldU>.IsEnabled = false;
            <ExitLoop>
            <IsNotIdentityCount>
            <And>
            <IsNotPKeyCount>
            <And>
            <IsMultilineTextBox>
            mtb<FieldU>.IsEnabled = false;
            <ExitLoop>
            <IsNotIdentityCount>
            <And>
            <IsNotPKeyCount>
            <And>
            <IsRichTextBox>
            rtb<FieldU>.IsEnabled = false;
            <ExitLoop>
            <IsNotIdentityCount>
            <And>
            <IsNotPKeyCount>
            <And>
            <IsComboBox (Db)>
            cb<FieldU>.IsEnabled = false;
            <ExitLoop>
            </If Loop>
            <If Loop>
            <IsNotIdentity>
            <And>
            <IsNotPKey>
            <And>
            <IsCheckBox>
            cb<FieldU>.Focus();
            <ExitLoop>
            <IsNotIdentity>
            <And>
            <IsNotPKey>
            <And>
            <IsNumericUpDown>
            nud<FieldU>.Focus();
            <ExitLoop>
            <IsNotIdentity>
            <And>
            <IsNotPKey>
            <And>
            <IsDateTimePicker>
            dtp<FieldU>.Focus();
            <ExitLoop>
            <IsNotIdentity>
            <And>
            <IsNotPKey>
            <And>
            <IsTextBox>
            tb<FieldU>.Focus();
            <ExitLoop>
            <IsNotIdentity>
            <And>
            <IsNotPKey>
            <And>
            <IsMaskedTextBox>
            mtb<FieldU>.Focus();
            <ExitLoop>
            <IsNotIdentity>
            <And>
            <IsNotPKey>
            <And>
            <IsMultilineTextBox>
            mtb<FieldU>.Focus();
            <ExitLoop>
            <IsNotIdentity>
            <And>
            <IsNotPKey>
            <And>
            <IsRichTextBox>
            rtb<FieldU>.Focus();
            <ExitLoop>
            <IsNotIdentity>
            <And>
            <IsNotPKey>
            <And>
            <IsComboBox>
            cb<FieldU>.Focus();
            <ExitLoop>
            <IsNotIdentity>
            <And>
            <IsNotPKey>
            <And>
            <IsComboBox (Db)>
            cb<FieldU>.Focus();
            <ExitLoop>
            </If Loop>
-----------------------------------------------------<Comment>
        <If Loop>
           <IsFKey>
           <And>
           <IsNotComboBox (Db)>
           <And>
           <IsNumber>
            nud<FieldU>.IsEnabled = false;
           <IsFKey>
           <And>
           <IsNotComboBox (Db)>
           <And>
           <IsNotNumber>
            tb<FieldU>.IsEnabled = false;
        </If Loop>
        }

        private void Delete()
        {
            AddMode = false;
            EditMode = false;
            DeleteMode = true;

            GetData();

            IsReadOnlyRecord(false);
        }

        private void IsReadOnlyRecord(bool YesNo)
        {
            <If Loop>
            <IsNotIdentity>
            <And>
            <IsCheckBox>
            cb<FieldU>.IsEnabled = YesNo;
            <IsNotIdentity>
            <And>
            <IsNumericUpDown>
            nud<FieldU>.IsEnabled = YesNo;
            <IsNotIdentity>
            <And>
            <IsDateTimePicker>
            dtp<FieldU>.IsEnabled = YesNo;
            <IsNotIdentity>
            <And>
            <IsTextBox>
            tb<FieldU>.IsEnabled = YesNo;
            <IsNotIdentity>
            <And>
            <IsMaskedTextBox>
            mtb<FieldU>.IsEnabled = YesNo;
            <IsNotIdentity>
            <And>
            <IsMultilineTextBox>
            mtb<FieldU>.IsEnabled = YesNo;
            <IsNotIdentity>
            <And>
            <IsRichTextBox>
            rtb<FieldU>.IsEnabled = YesNo;
            <IsNotIdentity>
            <And>
            <IsPictureBox>
            pb<FieldU>.IsEnabled = YesNo;
            <IsNotIdentity>
            <And>
            <IsComboBox>
            cb<FieldU>.IsEnabled = YesNo;
            <IsNotIdentity>
            <And>
            <IsComboBox (Db)>
            cb<FieldU>.IsEnabled = YesNo;
            </If Loop>
        }

        private void ClearRecord()
        {
            <If Loop>
            <IsCheckBox>
            cb<FieldU>.IsChecked = false;
            <IsNumericUpDown>
            nud<FieldU>.Text = "0";
            <IsDateTimePicker>
            dtp<FieldU>.Text = null;
            <IsTextBox>
            tb<FieldU>.Text = null;
            <IsMaskedTextBox>
            mtb<FieldU>.Text = null;
            <IsMultilineTextBox>
            mtb<FieldU>.Text = null;
            <IsRichTextBox>
            rtb<FieldU>.Text = null;
            <IsPictureBox>
            pb<FieldU>.Text = null;
            <IsComboBox>
            cb<FieldU>.SelectedIndex = -1;
            <IsComboBox (Db)>
            cb<FieldU>.SelectedIndex = -1;
            </If Loop>
        }

        private void GoBack_To_Grid()
        {
            int LastRow = Row;
            bool gridOK = false;
            try
            {
                LoadGrid(false);
                Grid.SelectedItems.Clear();
                Grid.SelectedItem = Grid.Items[LastRow];
                Grid.ScrollIntoView(Grid.Items[LastRow]);
            	tiGrid.IsSelected = true;
            	InitToolBarItems();
            	gridOK = true;
            }
            catch
            {
                //Hide error message.
            }
            finally
            {
                if (gridOK == false)
                {
                    LoadGrid(true);
                    tiGrid.IsSelected = true;
                    InitToolBarItems();
                }
            }
        }

        private void InsertRecord()
        {
            using (new WaitCursor())
            {
            <OwnerU>_<TableU>Class cls<OwnerU>_<TableU> = new <OwnerU>_<TableU>Class();
                if (VerifyData() == true) {
                    SetData(cls<OwnerU>_<TableU>);
                    Boolean bSucess = new Boolean();
                    bSucess = <OwnerU>_<TableU>DataClass.Add(cls<OwnerU>_<TableU>);
                    if (bSucess == true) {
                        GoBack_To_Grid(); }
                    else {
                        MessageBox.Show("Insert failed.", "Error"); }
                }
            }
        }

        private void UpdateRecord()
        {
            using (new WaitCursor())
            {
                <OwnerU>_<TableU>Class ocls<OwnerU>_<TableU> = new <OwnerU>_<TableU>Class();
                <OwnerU>_<TableU>Class cls<OwnerU>_<TableU> = new <OwnerU>_<TableU>Class();

        <If Loop>
           <IsPKey>
                ocls<OwnerU>_<TableU>.<FieldU> = <Convert>((Grid.SelectedCells[<ColNo>].Column.GetCellContent(Grid.SelectedItem) as TextBlock).Text);
        </If Loop>
        <If Loop>
           <IsNotPKey>
           <And>
           <IsIdentity>
                ocls<OwnerU>_<TableU>.<FieldU> = <Convert>((Grid.SelectedCells[<ColNo>].Column.GetCellContent(Grid.SelectedItem) as TextBlock).Text);
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
                ocls<OwnerU>_<TableU>.<FieldU> = <Convert>((Grid.SelectedCells[<ColNo>].Column.GetCellContent(Grid.SelectedItem) as TextBlock).Text);
           <ExitLoop>
        </If Loop>
                ocls<OwnerU>_<TableU> = <OwnerU>_<TableU>DataClass.Select_Record(ocls<OwnerU>_<TableU>);

                if (VerifyData() == true) {
                    SetData(cls<OwnerU>_<TableU>);
                    Boolean bSucess = new Boolean();
                    bSucess = <OwnerU>_<TableU>DataClass.Update(ocls<OwnerU>_<TableU>, cls<OwnerU>_<TableU>);
                    if (bSucess == true) {
                        GoBack_To_Grid(); }
                    else {
                        MessageBox.Show("Update failed.", "Error");}
                }
            }
        } 

        private void DeleteRecord()
        {
            using (new WaitCursor())
            {
                <OwnerU>_<TableU>Class cls<OwnerU>_<TableU> = new <OwnerU>_<TableU>Class();
        <If Loop>
           <IsPKey>
                cls<OwnerU>_<TableU>.<FieldU> = <Convert>((Grid.SelectedCells[<ColNo>].Column.GetCellContent(Grid.SelectedItem) as TextBlock).Text);
        </If Loop>
        <If Loop>
           <IsNotPKey>
           <And>
           <IsIdentity>
                cls<OwnerU>_<TableU>.<FieldU> = <Convert>((Grid.SelectedCells[<ColNo>].Column.GetCellContent(Grid.SelectedItem) as TextBlock).Text);
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
                cls<OwnerU>_<TableU>.<FieldU> = <Convert>((Grid.SelectedCells[<ColNo>].Column.GetCellContent(Grid.SelectedItem) as TextBlock).Text);
           <ExitLoop>
        </If Loop>
                if (MessageBox.Show("Are you sure? Delete this record?", "Delete", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) {
                    SetData(cls<OwnerU>_<TableU>);
                    Boolean bSucess = new Boolean();
                    bSucess = <OwnerU>_<TableU>DataClass.Delete(cls<OwnerU>_<TableU>);
                    if (bSucess == true) {
                        GoBack_To_Grid(); }
                    else {
                        MessageBox.Show("Delete failed.", "Error"); }
                }
            }
        }  

        private Boolean VerifyData()
        {
            <If Loop>
            <IsNotIdentity>
            <And>
            <IsNotNull>
            <And>
            <IsCheckBox>
            
            <IsNotIdentity>
            <And>
            <IsNotNull>
            <And>
            <IsNumericUpDown>
            if (nud<FieldU>.Text == "") {
                MessageBox.Show("<FieldC> is Required", "Error");
                nud<FieldU>.Focus();
                return false;}
            <IsNotIdentity>
            <And>
            <IsNotNull>
            <And>
            <IsDateTimePicker>
            
            <IsNotIdentity>
            <And>
            <IsNotNull>
            <And>
            <IsTextBox>
            if (tb<FieldU>.Text == "") {
                MessageBox.Show("<FieldC> is Required", "Error");
                tb<FieldU>.Focus();
                return false;}
            <IsNotIdentity>
            <And>
            <IsNotNull>
            <And>
            <IsMaskedTextBox>
            if (mtb<FieldU>.Text == "") {
                MessageBox.Show("<FieldC> is Required", "Error");
                mtb<FieldU>.Focus();
                return false;}
            <IsNotIdentity>
            <And>
            <IsNotNull>
            <And>
            <IsMultilineTextBox>
            if (mtb<FieldU>.Text == "") {
                MessageBox.Show("<FieldC> is Required", "Error");
                mtb<FieldU>.Focus();
                return false;}
            <IsNotIdentity>
            <And>
            <IsNotNull>
            <And>
            <IsRichTextBox>
            if (rtb<FieldU>.Text == "") {
                MessageBox.Show("<FieldC> is Required", "Error");
                rtb<FieldU>.Focus();
                return false;}
            <IsNotIdentity>
            <And>
            <IsNotNull>
            <And>
            <IsPictureBox>
            
            <IsNotIdentity>
            <And>
            <IsNotNull>
            <And>
            <IsComboBox>
            if (cb<FieldU>.SelectedIndex == -1) {
                MessageBox.Show("<FieldC> is Required", "Error");
                cb<FieldU>.Focus();
                return false;}
            <IsNotIdentity>
            <And>
            <IsNotNull>
            <And>
            <IsComboBox (Db)>
            if (cb<FieldU>.SelectedIndex == -1) {
                MessageBox.Show("<FieldC> is Required", "Error");
                cb<FieldU>.Focus();
                return false;}          
            </If Loop>
---------------------------------------<Comment>
            <If Loop>
	            <IsNotIdentityCount>
            if (
	            <ExitLoop>
            </If Loop>
            <If Loop>
	            <IsNotIdentity>
	            <And>
	            <IsPKey>
	            <And>
	            <IsNumericUpDown>
            nud<FieldU>.Text != "" 
	            <ExitLoop>
             	    <IsNotIdentity>
	            <And>
	            <IsPKey>
	            <And>
	            <IsTextBox>
            tb<FieldU>.Text != "" 
	            <ExitLoop>
	            <IsNotIdentity>
	            <And>
	            <IsPKey>
	            <And>
	            <IsMaskedTextBox>
            mtb<FieldU>.Text != "" 
	            <ExitLoop>
	            <IsNotIdentity>
	            <And>
	            <IsPKey>
	            <And>
	            <IsMultilineTextBox>
            mtb<FieldU>.Text != "" 
	            <ExitLoop>
	            <IsNotIdentity>
	            <And>
	            <IsPKey>
	            <And>
	            <IsRichTextBox>
            rtb<FieldU>.Text != "" 
	            <ExitLoop>
	            <IsNotIdentity>
	            <And>
	            <IsPKey>
	            <And>
	            <IsComboBox (Db)>
            cb<FieldU>.SelectedIndex != -1 
	            <ExitLoop>
            </If Loop>
            <If Loop>
	            <IsNotIdentity>
	            <And>
	            <IsPKey>
	            <And>
	            <IsNumericUpDown>
           	    <And>
           	    <IsCurRow<>[0]>
            && nud<FieldU>.Text != "" 
             	    <IsNotIdentity>
	            <And>
	            <IsPKey>
	            <And>
	            <IsTextBox>
           	    <And>
           	    <IsCurRow<>[0]>
            && tb<FieldU>.Text != "" 
	            <IsNotIdentity>
	            <And>
	            <IsPKey>
	            <And>
	            <IsMaskedTextBox>
           	    <And>
           	    <IsCurRow<>[0]>
            && mtb<FieldU>.Text != "" 
	            <IsNotIdentity>
	            <And>
	            <IsPKey>
	            <And>
	            <IsMultilineTextBox>
           	    <And>
           	    <IsCurRow<>[0]>
            && mtb<FieldU>.Text != "" 
	            <IsNotIdentity>
	            <And>
	            <IsPKey>
	            <And>
	            <IsRichTextBox>
           	    <And>
           	    <IsCurRow<>[0]>
            && rtb<FieldU>.Text != "" 
	            <IsNotIdentity>
	            <And>
	            <IsPKey>
	            <And>
	            <IsComboBox (Db)>
           	    <And>
           	    <IsCurRow<>[0]>
            && cb<FieldU>.SelectedIndex != -1 
            </If Loop>
            <If Loop>
	            <IsNotIdentityCount>
	            <And>
	            <IsNotPKeyCount>
	            <And>
	            <IsNumericUpDown>
            nud<FieldU>.Text != "" 
	            <ExitLoop>
	            <IsNotIdentityCount>
	            <And>
	            <IsNotPKeyCount>
	            <And>
	            <IsTextBox>
            tb<FieldU>.Text != "" 
	            <ExitLoop>
	            <IsNotIdentityCount>
	            <And>
	            <IsNotPKeyCount>
	            <And>
	            <IsMaskedTextBox>
            mtb<FieldU>.Text != "" 
	           <ExitLoop>
	            <IsNotIdentityCount>
	            <And>
	            <IsNotPKeyCount>
	            <And>
	            <IsMultilineTextBox>
            mtb<FieldU>.Text != "" 
	           <ExitLoop>
	            <IsNotIdentityCount>
	            <And>
	            <IsNotPKeyCount>
	            <And>
	            <IsRichTextBox>
            rtb<FieldU>.Text != "" 
	            <ExitLoop>
	            <IsNotIdentityCount>
	            <And>
	            <IsNotPKeyCount>
	            <And>
	            <IsComboBox (Db)>
            cb<FieldU>.SelectedIndex != -1 
        	    <ExitLoop>
	            </If Loop>
            <If Loop>
	            <IsNotIdentityCount>
            )  {
            <OwnerU>_<TableU>Class cls<OwnerU>_<TableU> = new <OwnerU>_<TableU>Class();
	            <ExitLoop>
            </If Loop>
            <If Loop>
	            <IsNotIdentity>
	            <And>
	            <IsPKey>
	            <And>
	            <IsNumericUpDown>
            cls<OwnerU>_<TableU>.<FieldU> = <Convert>(nud<FieldU>.Text);
	            <IsNotIdentity>
	            <And>
	            <IsPKey>
	            <And>
	            <IsTextBox>
            cls<OwnerU>_<TableU>.<FieldU> = <Convert>(tb<FieldU>.Text);
	            <IsNotIdentity>
	            <And>
	            <IsPKey>
	            <And>
	            <IsMaskedTextBox>
            cls<OwnerU>_<TableU>.<FieldU> = <Convert>(mtb<FieldU>.Text);
	            <IsNotIdentity>
	            <And>
	            <IsPKey>
	            <And>
	            <IsMultilineTextBox>
            cls<OwnerU>_<TableU>.<FieldU> = <Convert>(mtb<FieldU>.Text);
	            <IsNotIdentity>
	            <And>
	            <IsPKey>
	            <And>
	            <IsRichTextBox>
            cls<OwnerU>_<TableU>.<FieldU> = <Convert>(rtb<FieldU>.Text);
	            <IsNotIdentity>
	            <And>
	            <IsPKey>
	            <And>
        	    <IsComboBox (Db)>
            cls<OwnerU>_<TableU>.<FieldU> = <Convert>(cb<FieldU>.SelectedValue);
            </If Loop>
            <If Loop>
	            <IsNotIdentityCount>
	            <And>
	            <IsNotPKeyCount>
	            <And>
	            <IsNumericUpDown>
            cls<OwnerU>_<TableU>.<FieldU> = <Convert>(nud<FieldU>.Text);
	            <ExitLoop>
	            <IsNotIdentityCount>
	            <And>
	            <IsNotPKeyCount>
	            <And>
	            <IsTextBox>
            cls<OwnerU>_<TableU>.<FieldU> = <Convert>(tb<FieldU>.Text);
	            <ExitLoop>
	            <IsNotIdentityCount>
	            <And>
	            <IsNotPKeyCount>
	            <And>
	            <IsMaskedTextBox>
            cls<OwnerU>_<TableU>.<FieldU> = <Convert>(mtb<FieldU>.Text);
	            <ExitLoop>
	            <IsNotIdentityCount>
	            <And>
	            <IsNotPKeyCount>
	            <And>
	            <IsMultilineTextBox>
            cls<OwnerU>_<TableU>.<FieldU> = <Convert>(mtb<FieldU>.Text);
	            <ExitLoop>
	            <IsNotIdentityCount>
	            <And>
	            <IsNotPKeyCount>
	            <And>
	            <IsRichTextBox>
            cls<OwnerU>_<TableU>.<FieldU> = <Convert>(rtb<FieldU>.Text);
	            <ExitLoop>
	            <IsNotIdentityCount>
	            <And>
	            <IsNotPKeyCount>
	            <And>
	            <IsComboBox (Db)>
            cls<OwnerU>_<TableU>.<FieldU> = <Convert>(cb<FieldU>.SelectedValue);
        	    <ExitLoop>
            </If Loop>
            <If Loop>
	            <IsNotIdentityCount>
            cls<OwnerU>_<TableU> = <OwnerU>_<TableU>DataClass.Select_Record(cls<OwnerU>_<TableU>);
            if (cls<OwnerU>_<TableU> != null && this.AddMode == true) {
                 MessageBox.Show("Record already exists", "Error");
	            <ExitLoop>
            </If Loop>
            <If Loop>
            <IsNotIdentity>
            <And>
            <IsPKey>
            <And>
            <IsNumericUpDown>
                 nud<FieldU>.Focus();
            <ExitLoop>
            <IsNotIdentity>
            <And>
            <IsPKey>
            <And>
            <IsTextBox>
                 tb<FieldU>.Focus();
            <ExitLoop>
            <IsNotIdentity>
            <And>
            <IsPKey>
            <And>
            <IsMaskedTextBox>
                 mtb<FieldU>.Focus();
            <ExitLoop>
            <IsNotIdentity>
            <And>
            <IsPKey>
            <And>
            <IsMultilineTextBox>
                   mtb<FieldU>.Focus();
            <ExitLoop>
            <IsNotIdentity>
            <And>
            <IsPKey>
            <And>
            <IsRichTextBox>
                   rtb<FieldU>.Focus();
            <ExitLoop>
            <IsNotIdentity>
            <And>
            <IsPKey>
            <And>
            <IsComboBox (Db)>
                   cb<FieldU>.Focus();
            <ExitLoop>
            </If Loop>
            <If Loop>
            <IsNotIdentityCount>
            <And>
            <IsNotPKeyCount>
            <And>
            <IsNumericUpDown>
                   nud<FieldU>.Focus();
            <ExitLoop>
            <IsNotIdentityCount>
            <And>
            <IsNotPKeyCount>
            <And>
            <IsTextBox>
                  tb<FieldU>.Focus();
            <ExitLoop>
            <IsNotIdentityCount>
            <And>
            <IsNotPKeyCount>
            <And>
            <IsMaskedTextBox>
                   mtb<FieldU>.Focus();
            <ExitLoop>
            <IsNotIdentityCount>
            <And>
            <IsNotPKeyCount>
            <And>
            <IsMultilineTextBox>
                  mtb<FieldU>.Focus();
            <ExitLoop>
            <IsNotIdentityCount>
            <And>
            <IsNotPKeyCount>
            <And>
            <IsRichTextBox>
                  rtb<FieldU>.Focus();
            <ExitLoop>
            <IsNotIdentityCount>
            <And>
            <IsNotPKeyCount>
            <And>
            <IsComboBox (Db)>
                   cb<FieldU>.Focus();
            <ExitLoop>
            </If Loop>
            <If Loop>
            <IsNotIdentityCount>
                  return false; }
            <ExitLoop>
            </If Loop>
            <If Loop>
            <IsNotIdentityCount>
            }
            <ExitLoop>
            </If Loop>
---------------------------------------<Comment>
            return true;
        }

        <If Loop>
           <IsNumericUpDown>
        private void nud<FieldU>_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
        {
            if (!char.IsDigit(Convert.ToChar(e.Text)))
                e.Handled = true;
        }

        </If Loop>

        private void cbExport_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            using (new WaitCursor())
            {
                try
                {
                    <Database>DataClass cls<Database>Data = new <Database>DataClass();
                    if (cbiPDF.IsSelected == true)
                    {
                        if (tiGrid.IsSelected)
                        {
                            cls<Database>Data.ExportToPdf("Many", 
                                                     "<Head>", Grid, (DataRowView)Grid.CurrentItem, Row);
                        }
                        else if (tiDetail.IsSelected)
                        {
                            cls<Database>Data.ExportToPdf("One", 
                                                     "<Head>", Grid, (DataRowView)Grid.CurrentItem, Row);
                        }
                    }
                    else if (cbiExcel.IsSelected == true)
                    {
                        if (tiGrid.IsSelected)
                        {
                            cls<Database>Data.ExportToExcel("Many", 
                                                     "<Head>", Grid, (DataRowView)Grid.CurrentItem, Row);
                        }
                        else if (tiDetail.IsSelected)
                        {
                            cls<Database>Data.ExportToExcel("One", 
                                                     "<Head>", Grid, (DataRowView)Grid.CurrentItem, Row);
                        }
                    }
                    else if (cbiWord.IsSelected == true)
                    {
                        if (tiGrid.IsSelected)
                        {
                            cls<Database>Data.ExportToWord("Many", 
                                                     "<Head>", Grid, (DataRowView)Grid.CurrentItem, Row);
                        }
                        else if (tiDetail.IsSelected)
                        {
                            cls<Database>Data.ExportToWord("One", 
                                                     "<Head>", Grid, (DataRowView)Grid.CurrentItem, Row);
                        }
                    }
                    cbiExport.IsSelected = true;
                }
                catch
                {
                }
            }
        }

        private void butShowAll_Click(object sender, RoutedEventArgs e)
        {
            txtSearch.Text = null;
            LoadGrid(true);
            tiGrid.IsSelected = true;
        }

        private void butSearch_Click(object sender, RoutedEventArgs e)
        {
            using (new WaitCursor())
            {
                try
                {
                    DataTable dt = <OwnerU>_<TableU>DataClass.Search(System.Convert.To<DRType>(<DRField>), cmbFields.Text, cmbCondition.Text, txtSearch.Text);
                    Grid.ItemsSource = dt.DefaultView;
                    Grid.CanUserAddRows = false;
                    Grid.CanUserDeleteRows = false;
                    Grid.IsReadOnly = true;
                    if (Grid.Items.Count > 0)
                    {
                        Grid.SelectedItem = Grid.Items[0];
                        Grid.ScrollIntoView(Grid.SelectedItem);
                    }
                }
                catch
                {
                    MessageBox.Show("An error occurred in butSearch_Click...", "Error");
                }
                finally
                {
                }
            }
        }

    }
}





